import { isWechat } from '@/libs/utils'
import wx from 'weixin-js-sdk'
const certificateMixin = {
  methods: {
    certificateCheck (certificate) {
      return new Promise((resolve, reject) => {
        if (isWechat() && certificate) {
          if (!this.$store.getters.getUserInfo.phoneNum || this.$store.getters.getUserInfo.phoneNum === '-1') {
            this.$toast('请先认证')
            this.$router.push({
              name: 'certificate'
            })
            reject(new Error('请先认证'))
          } else {
            resolve(true)
          }
        } else {
          if (!this.$store.state.account.accessToken) {
            this.$store.dispatch('logout')
            reject(new Error('请先登录'))
          } else {
            resolve(true)
          }
        }
      })
    }
  }
}

const wxjsdkMixin = {
  methods: {
    openWechatLocation (point) {
      // console.log(point)
      wx.ready(() => {
        wx.openLocation({
          latitude: parseFloat(point.lat), // 纬度，浮点数，范围为90 ~ -90
          longitude: parseFloat(point.lng), // 经度，浮点数，范围为180 ~ -180。
          name: point.name, // 位置名
          address: point.address, // 地址详情说明
          scale: 14, // 地图缩放级别,整形值,范围从1~28。默认为最大
          infoUrl: '', // 在查看位置界面底部显示的超链接,可点击跳转
          complete (res) {
            // alert(JSON.stringify(res))
          }
        })
      })
    },
    wechatShare (options) {
      if (isWechat()) {
        wx.ready(() => {
          wx.onMenuShareAppMessage ? wx.onMenuShareAppMessage(options) : wx.updateAppMessageShareData(options)
          wx.onMenuShareTimeline ? wx.onMenuShareTimeline(options) : wx.updateTimelineShareData(options)
        })
      }
    },
    wechatHideMenu () {
      if (isWechat()) {
        wx.ready(() => {
          console.log('hide menu')
          wx.hideAllNonBaseMenuItem()
        })
      }
    },
    wechatShowMenu () {
      if (isWechat()) {
        wx.ready(() => {
          console.log('show menu')
          wx.showAllNonBaseMenuItem()
        })
      }
    },
    getWxLocation (callback) {
      wx.ready(() => {
        wx.getLocation({
          type: 'gcj02',
          success (res) {
            // console.log('get wx position', res)
            let lat = res.latitude
            let lng = res.longitude
            callback && callback({ lat, lng })
          },
          complete (res) {
            // alert(JSON.stringify(res))
          }
        })
      })
    }
  }
}

export {
  certificateMixin,
  wxjsdkMixin
}
